Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AllowPrivilegeEscalation: add validations #52803

Merged

Conversation

jessfraz
Copy link
Contributor

Fixes #52454

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Sep 20, 2017
@jessfraz jessfraz added this to the v1.8 milestone Sep 20, 2017
@k8s-github-robot k8s-github-robot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Sep 20, 2017
@dims
Copy link
Member

dims commented Sep 20, 2017

/assign @lavalamp

@dims
Copy link
Member

dims commented Sep 20, 2017

/release-note-none

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Sep 20, 2017
@jessfraz
Copy link
Contributor Author

jessfraz commented Sep 20, 2017 via email


// handle the case for AllowPrivilegeEscalation when the container is privileged
if sc.Privileged != nil && *sc.Privileged {
sc.AllowPrivilegeEscalation = &pt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure it's acceptable for defaulting code to change values that have been set. Maybe it would be better for this to go in validation (i.e. setting AllowPrivilegeEscalataion == false && Privileged == true is an error)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in a differnt commit, can squash before merge, added tests too

@@ -69,7 +69,8 @@ func (m *kubeGenericRuntimeManager) determineEffectiveSecurityContext(pod *v1.Po
synthesized.SupplementalGroups = append(synthesized.SupplementalGroups, groups...)
}

synthesized.NoNewPrivs = securitycontext.AddNoNewPrivileges(effectiveSc)
// Set the NoNewPrivs setting to the opposite of AllowPrivilegeEscalation.
synthesized.NoNewPrivs = !*effectiveSc.AllowPrivilegeEscalation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: handle the nil case. e.g. for static pods (just default to true?)

@jessfraz jessfraz force-pushed the AllowPrivilegeEscalation branch 2 times, most recently from 90d9903 to 0b4086e Compare September 20, 2017 18:37
@dims
Copy link
Member

dims commented Sep 20, 2017

@jessfraz are these legit failures? (let me try a retest in case they are flakes)

/retest

@jessfraz
Copy link
Contributor Author

jessfraz commented Sep 20, 2017

Do you know why TestRoundTripTypes could have broken, it looks like it's doing a fuzz so I'm unsure how the Defaults behavior might have messed with that?

update: figured this one out but the other mystery remains with the e2e-node tests

@dims
Copy link
Member

dims commented Sep 20, 2017

ah fuzzers!

/test pull-kubernetes-node-e2e

}

if sc.AllowPrivilegeEscalation != nil && !*sc.AllowPrivilegeEscalation {
if *sc.Privileged {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jess, found the trace backs : http://paste.openstack.org/show/621629/

Looks like we need to check for nil here

if sc.Privileged != nil && *sc.Privileged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omg you are my new favorite person on the planet

@dims
Copy link
Member

dims commented Sep 21, 2017

@jessfraz please see inline comment, found the panic traceback

@jessfraz jessfraz force-pushed the AllowPrivilegeEscalation branch 2 times, most recently from 26506c0 to 2fa9bf5 Compare September 21, 2017 15:00
@@ -389,3 +389,32 @@ func SetDefaults_HostPathVolumeSource(obj *v1.HostPathVolumeSource) {
obj.Type = &typeVol
}
}

func SetDefaults_SecurityContext(sc *v1.SecurityContext) {
pt := true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, it should have name ape (as in the fuzzer).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do, I was debating over what naming I liked better, honestly I just wish &true and &false worked

if sc.Capabilities != nil {
for _, cap := range sc.Capabilities.Add {
if string(cap) == "CAP_SYS_ADMIN" {
sc.AllowPrivilegeEscalation = &pt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break is missing here.

}

// handle the case for AllowPrivilegeEscalation when we are adding CAP_SYS_ADMIN
if sc.Capabilities != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if can be replaced by else if to prevent inspecting capabilities when we've already set AllowPrivilegeEscalation to true in the previous branch.

}

// handle the case where the user did not set the defaultAllowPrivilegeEscalation and did not explicitly set allowPrivilegeEscalation
if sc.AllowPrivilegeEscalation == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment assumes that defaultAllowPrivilegeEscalation == nil but code doesn't check for that. Is it intentionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I was just trying to relay that the securityContext would be nil if they didn't set a default.

"allowPrivilegeEscalation nil security context nil": {
expect: true,
},
"allowPrivilegeEscalation nil capAddSysadmin": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/nil capAddSysadmin/and CAP_SYS_ADMIN/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see the logic in the names now... Could you make it more explicit? Otherwise it could be misinterpreted which exactly value is nil in the cases like allowPrivilegeEscalation nil privileged

if sc.Capabilities != nil {
for _, cap := range sc.Capabilities.Add {
if string(cap) == "CAP_SYS_ADMIN" {
allErrs = append(allErrs, field.Forbidden(field.NewPath(""), "Cannot set `allowPrivilegeEscalation` to false and `capAdd` CAP_SYS_ADMIN"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no field capAdd. Let's use capabilities.add at least.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plase, add break here to stop early.

@tallclair
Copy link
Member

/lgtm
Thanks!


if sc.AllowPrivilegeEscalation != nil && !*sc.AllowPrivilegeEscalation {
if sc.Privileged != nil && *sc.Privileged {
allErrs = append(allErrs, field.Invalid(fldPath, sc, "Cannot set `allowPrivilegeEscalation` to false and `privileged` to true"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error string start with lower case?

if sc.Capabilities != nil {
for _, cap := range sc.Capabilities.Add {
if string(cap) == "CAP_SYS_ADMIN" {
allErrs = append(allErrs, field.Invalid(fldPath, sc, "Cannot set `allowPrivilegeEscalation` to false and `capabilities.Add` CAP_SYS_ADMIN"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

@dchen1107
Copy link
Member

Small nits, otherwise look good to me.

@liggitt
Copy link
Member

liggitt commented Sep 25, 2017

this is release blocking... can we merge/pick this as-is for 1.8, and follow up with the message tweaks?

@jessfraz
Copy link
Contributor Author

I can update the PR real fast no worries

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
@k8s-github-robot k8s-github-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 25, 2017
@liggitt
Copy link
Member

liggitt commented Sep 25, 2017

/lgtm
marking approved per #52803 (comment)

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 25, 2017
@liggitt liggitt added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 25, 2017
@liggitt liggitt modified the milestones: v1.8, v1.9 Sep 25, 2017
@dchen1107
Copy link
Member

/lgtm

@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dchen1107, dims, jessfraz, liggitt, tallclair

Associated issue: 52454

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@jessfraz
Copy link
Contributor Author

/test pull-kubernetes-e2e-gce-bazel

@abgworrall abgworrall added cherry-pick-approved Indicates a cherry-pick PR into a release branch has been approved by the release branch manager. and removed cherrypick-candidate labels Sep 25, 2017
@abgworrall
Copy link
Contributor

Changing cherrypick- labels, to account for the fact that this master PR has already been cherrypicked into a base:release-1.8 PR and sent on to the submit queue, and so doesn't need attention from the batch cherrypick tooling.

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 51067, 52319, 52803, 52961, 51972). If you want to cherry-pick this change to another branch, please follow the instructions here..

@k8s-github-robot k8s-github-robot merged commit 3090fe5 into kubernetes:master Sep 25, 2017
@jessfraz jessfraz deleted the AllowPrivilegeEscalation branch September 25, 2017 21:51
k8s-github-robot pushed a commit that referenced this pull request Sep 26, 2017
Automatic merge from submit-queue.

Cherry-pick #52803: AllowPrivilegeEscalation: add validations

Cherry-pick of #52803: AllowPrivilegeEscalation: add validations

Fixes #52454
k8s-github-robot pushed a commit that referenced this pull request Feb 8, 2018
…n_test

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Move test cases to a proper test method

**What this PR does / why we need it**:
Prior this change, we had tests in `TestValidatePodSpec()` method that is designated for testing `ValidatePodSpec()`. But because we test code from the `ValidateSecurityContext()` method, the tests should belong to `TestValidateSecurityContext()`.

This PR improves the tests. Now the tests become less fragile because `ValidatePodSpec()` do a lot more validations than `ValidateSecurityContext()`.

**Which issue(s) this PR fixes**:
Related to #52803 where this code and tests were introduced.

**Release note**:
```release-note
NONE
```

PTAL @jessfraz 
CC @simo5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/api Indicates an issue on api area. cherry-pick-approved Indicates a cherry-pick PR into a release branch has been approved by the release branch manager. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. sig/auth Categorizes an issue or PR as relevant to SIG Auth. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet